package com.lcf.system.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.lcf.base.exception.SysException;
import com.lcf.framework.model.system.SysDict;
import com.lcf.system.dto.dict.SysDictDTO;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

/**
 * 字典
 *
 * @author lcf
 */
@Component
public class SysDictManager {
    /**
     * 检查字典是否存在
     */
    public void checkExist(Long id, SysDictDTO dto) {
        SysDict sysDict = Db.lambdaQuery(SysDict.class)
                .eq(SysDict::getTypeId, dto.getTypeId())
                .and(x -> x.eq(SysDict::getDictValue, dto.getDictValue())
                        .or().eq(SysDict::getDictName, dto.getDictName()))
                .ne(id != null, SysDict::getId, id)
                .one();

        if (sysDict != null) {
            throw new SysException("字典已存在");
        }
    }

    /**
     * 检查id
     */
    public SysDict checkId(long id) {
        return checkId(Collections.singletonList(id)).get(0);
    }

    /**
     * 检查id
     */
    public List<SysDict> checkId(List<Long> idList) {
        List<SysDict> list = Db.lambdaQuery(SysDict.class).in(SysDict::getId, idList).list();
        if (CollectionUtil.isEmpty(list) || list.size() != idList.size()) {
            throw new SysException("字典不存在");
        }

        return list;
    }
}
